//
//  Like.m
//  HYHOME
//
//  Created by Mac on 16/2/10.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "Like.h"
#import "UIImageView+WebCache.h"
#import "URLViewController.h"
#import "DataBaseHandle.h"
#import "Modelcontent.h"
#import "LikeCell.h"
#import "DKNightVersion.h"

@interface Like ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic,retain) UITableView *tableView;


@end

@implementation Like
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.array = [[DataBaseHandle shareDataBaseHandle] select].mutableCopy;
    [self.tableView reloadData];
    NSLog(@"%@", NSHomeDirectory());
    
    
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view from its nib.
    [self createCell];
    

    
    
    
}


- (void)createCell {
    
    
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sctp.jpg"]];
    
    [self.view addSubview:imageView];
    
    imageView.userInteractionEnabled = YES;
    imageView.frame = self.view.frame;
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//
//    
    [imageView addSubview:self.tableView];
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor clearColor], [UIColor blackColor]);
//    self.tableView.dk_separatorColorPicker = DKColorWithRGB(0xaaaaaa, 0x313131);

    
    [self.tableView registerClass:[LikeCell class] forCellReuseIdentifier:@"pool2"];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;

    [self.tableView release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  self.array.count;
    
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   LikeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
    
    Modelcontent *model = [self.array objectAtIndex:indexPath.row];
     cell.textLabel.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor lightGrayColor]);
    
    
    if (cell.textLabel.text ==nil) {
        
        self.tableView.backgroundColor = [UIColor whiteColor];
    }else{
    
    
        self.tableView.backgroundColor = [UIColor clearColor];
    
    }
    
    
    [cell passModel:model];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
    
}







- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    
    
    URLViewController *url = [[URLViewController alloc]init];
    
    Modelcontent *model = [self.array objectAtIndex:indexPath.row];
    [url passSecondModel:model];
    
    
    [self.navigationController pushViewController:url animated:YES];
    
    [url release];

}



#pragma mark - 编辑删除
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    
    
    [super setEditing:editing animated:animated];
    

    [self.tableView setEditing:editing animated:animated];
    
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;



}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    
    NSLog(@"%@",self.model);

    if (UITableViewCellEditingStyleDelete == editingStyle) {
        
        
        [self.tableView reloadData];
        Modelcontent *model = [self.array objectAtIndex:indexPath.row];
        //从数据源删除
        [self.array removeObjectAtIndex:indexPath.row];
        //从 tableView 删除
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [[DataBaseHandle shareDataBaseHandle]deleteWithModel:model];
        
        self.tableView.backgroundColor = [UIColor clearColor];

        
        
        

    }




}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
